﻿using HKB.Common;
using HKB.Web;
using HKB.Web.Admin;
using System;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Admin_ShopClass : Admin, IRequiresSessionState
{
     
    public string m_Action = "";
    public string m_Script = "";
    public string m_UpdateSql = "";
    protected DefaultProfile profile
    {
        get
        {
            return (DefaultProfile)this.Context.Profile;
        }
    }
    protected global_asax applicationInstance
    {
        get
        {
            return (global_asax)this.Context.ApplicationInstance;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!base.admin.CheckAdminPower(20))
        {
            base.WriteLog("View", Language.GetAdminMsg("Log/Access", "0"), "Class");
            base.WriteRightErrMsg();
        }
        this.m_Action = Fetch.Get("Action");
        this.m_Script = "<SCRIPT language=JavaScript type=text/javascript>";
        this.m_Script += "var myClass =";
        this.m_Script += "[";
    }
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        this.script_literal.Text = this.m_Script + "[0,0,0,0]];</script>";
    }
    protected void Class_OnDeleting(object sender, ObjectDataSourceMethodEventArgs e)
    {
        int num = Convert.ToInt32(e.InputParameters["ClassID"]);
        if (num > 0)
        {
            Shop_Class shop_Class = new Shop_Class(this.conn);
            int num2 = shop_Class.CountClassNumByClassID(num);
            if (num2 > 0)
            {
                string[] myparam = new string[]
				{
					num2.ToString(),
					num.ToString()
				};
                this.WriteErrMsg("", Language.GetAdminMsg("ShopClass/ChildClassNumErr", myparam), "");
            }
            int num3 = shop_Class.CountProductNumByClassID(num);
            if (num3 > 0)
            {
                string[] myparam = new string[]
				{
					num3.ToString(),
					num.ToString()
				};
                this.WriteErrMsg("", Language.GetAdminMsg("ShopClass/ProductNumErr", myparam), "");
            }
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (!base.admin.CheckAdminPower(20))
            {
                LinkButton linkButton = (LinkButton)e.Row.FindControl("btnDelete");
                linkButton.Visible = false;
            }
            e.Row.Attributes.Add("onmouseover", "cc(this)");
            e.Row.Attributes.Add("onmouseout", "cc1(this)");
            e.Row.Attributes.Add("id", "tr" + DataBinder.Eval(e.Row.DataItem, "ClassID"));
            int num = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ClassID"));
            int num2 = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Child"));
            int num3 = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ParentID"));
            int num4 = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Depth"));
            if (num4 > 0)
            {
                e.Row.Attributes.Add("style", "display:''");
            }
            object script = this.m_Script;
            this.m_Script = string.Concat(new object[]
			{
				script,
				"[",
				num,
				",",
				num4,
				",",
				num2,
				",",
				num3,
				"],"
			});
        }
    }
    protected string DisplayClassName(DataRow hl_class)
    {
        string result;
        if (hl_class != null && hl_class.ToString() != "")
        {
            string text = "";
            for (int i = 1; i < Convert.ToInt32(hl_class["Depth"]); i++)
            {
                if (Convert.ToInt32(hl_class["OrderByID"]) < Convert.ToInt32(hl_class["ClassID"]))
                {
                    text += "<img src='Images/Tree/tree_I.gif'>";
                }
                else
                {
                    text += "<img src='Images/Tree/tree_empty.gif'>";
                }
            }
            if (Convert.ToInt32(hl_class["Depth"]) != 0)
            {
                text += "<img src='Images/Tree/tree_L.gif'>";
            }
            if (Convert.ToInt32(hl_class["Child"]) == 0)
            {
                text += "<img src='Images/Tree/tree_folderend.gif'>";
            }
            else
            {
                object obj = text;
                text = string.Concat(new object[]
				{
					obj,
					"<img src='Images/Tree/tree_folder_start.gif' onClick='showHide(",
					Convert.ToInt32(hl_class["ClassID"]),
					")'>"
				});
            }
            string text2 = "0";
            try
            {
                text2 = hl_class["ItemCount"].ToString();
            }
            catch
            {
            }
            string text3 = text;
            text = string.Concat(new string[]
			{
				text3,
				"<a href='/ProductList.aspx?ClassID=",
				hl_class["ClassID"].ToString(),
				"' target='_blank'>",
				hl_class["OrderByID"].ToString(),
				"、",
				hl_class["ClassName"].ToString(),
				"</a> (子分类：<b>",
				hl_class["Child"].ToString(),
				"</b>) (上架商品：<em>",
				text2,
				"</em>)"
			});
            result = text;
        }
        else
        {
            result = "";
        }
        return result;
    }
    protected void ObjectDataSource1_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
    {
        this.GridView1.DataSourceID = "ObjectDataSource1";
        this.GridView1.DataBind();
    }
    protected void RepairClassButton_Click(object sender, EventArgs e)
    {
        Shop_Class shop_Class = new Shop_Class(this.conn);
        shop_Class.RepairClass();
    }
    protected void UpdateClassSumButton_Click(object sender, EventArgs e)
    {
        if (!base.admin.CheckAdminPower(20))
        {
            base.WriteLog("Modify", Language.GetAdminMsg("Log/Access", "0"), "Class");
            base.WriteRightErrMsg();
        }
        Shop_Class shop_Class = new Shop_Class(this.conn);
        shop_Class.SumClassProductNum();
    }
    protected void MoveClass_Click(object sender, EventArgs e)
    {
        if (Text.IsNumber(this.MoveNums.Text) && this.MoveNums.Text != "0")
        {
            int changeOrder = Convert.ToInt32(this.MoveAction.SelectedValue + this.MoveNums.Text);
            Shop_Class shop_Class = new Shop_Class(this.conn);
            shop_Class.ChangeClassOrder(base.Request["moveClassID"], changeOrder);
        }
    }
    protected void CreateClassJSButton_Click(object sender, EventArgs e)
    {
        this.CreateJs();
    }
    protected void CreateJs()
    {
        string sql = "select ClassName,ClassID,ParentID,Child from hlshop_Class order by RootID,OrderByID,Depth asc";
        DataTable dataTable = this.conn.GetDataTable(sql);
        string text = "_class={";
        if (dataTable != null && dataTable.Rows.Count > 0)
        {
            foreach (DataRow dataRow in dataTable.Rows)
            {
                string text2 = text;
                text = string.Concat(new string[]
				{
					text2,
					"\"",
					dataRow["ClassID"].ToString(),
					"\":[\"",
					dataRow["ClassName"].ToString(),
					"\",\"",
					dataRow["ParentID"].ToString(),
					"\",\"",
					dataRow["Child"].ToString(),
					"\"],"
				});
            }
            text = text.Remove(text.Length - 1);
        }
        text += "}";
        string path = HttpContext.Current.Server.MapPath("/Js/HoWave_ShopClass.js");
        using (StreamWriter streamWriter = new StreamWriter(path, false, Encoding.UTF8))
        {
            streamWriter.WriteLine(text);
        }
        this.WriteSuccessMsg("", Language.Get("Common/Success"), "Admin_ShopClass.aspx");
    }
}
